#ifndef DYNAMICWATERMARK_H
#define DYNAMICWATERMARK_H

#include <QObject>
#include <QImage>
#include <QPainter>
#include <QTimer>
#include <QCryptographicHash>
#include <QRandomGenerator>
#include <vector>

// 动态水印系统 - 防破解的水印实现
class DynamicWatermark : public QObject
{
    Q_OBJECT

public:
    static DynamicWatermark& instance();

    // 水印样式枚举
    enum WatermarkStyle {
        Classic,     // 经典样式
        Modern,      // 现代样式
        Minimal,     // 简约样式
        Security,    // 安全样式
        Custom       // 自定义样式
    };

    // 水印配置
    struct WatermarkConfig {
        WatermarkStyle style = Classic;
        int opacity = 128;
        int fontSize = 36;
        QColor color = QColor(200, 200, 200);
        int rotation = -45;
        int gridX = 3;
        int gridY = 3;
        bool antiAliasing = true;
        bool dynamicPosition = true;
        bool timeBasedContent = true;
        bool hardwareBased = true;
    };

    // 主要接口 - 替代原有的简单水印函数
    bool shouldApplyWatermark();
    void applyWatermark(QImage& image);
    void applyWatermark(QPainter& painter, const QRect& rect);

    // 配置接口
    void setWatermarkConfig(const WatermarkConfig& config);
    WatermarkConfig getWatermarkConfig() const;

    // 动态内容生成
    QString generateWatermarkText();
    QFont generateWatermarkFont();
    QColor generateWatermarkColor();

private:
    explicit DynamicWatermark(QObject *parent = nullptr);
    ~DynamicWatermark();

    static DynamicWatermark* s_instance;
    WatermarkConfig m_config;
    QTimer* m_updateTimer;

    // 动态算法
    struct AlgorithmState {
        qint64 lastUpdate = 0;
        uint32_t seed = 0;
        QByteArray signature;
        int algorithmIndex = 0;
        bool isCompromised = false;
    };
    AlgorithmState m_algorithmState;

    // 多种水印算法
    void applyClassicWatermark(QPainter& painter, const QRect& rect, const QString& text);
    void applyModernWatermark(QPainter& painter, const QRect& rect, const QString& text);
    void applyMinimalWatermark(QPainter& painter, const QRect& rect, const QString& text);
    void applySecurityWatermark(QPainter& painter, const QRect& rect, const QString& text);
    void applyCustomWatermark(QPainter& painter, const QRect& rect, const QString& text);

    // 反删除机制
    void embedInvisibleMarkers(QPainter& painter, const QRect& rect);
    void addFrequencyDomainWatermark(QImage& image);
    void addPixelLevelWatermark(QImage& image);

    // 算法选择和切换
    WatermarkStyle selectBestAlgorithm();
    void rotateAlgorithm();
    bool isAlgorithmCompromised();

    // 验证机制
    bool validateWatermarkIntegrity();
    QByteArray calculateWatermarkHash(const QString& text);

    // 动态参数生成
    struct DynamicParams {
        QVector<QPoint> positions;
        QVector<int> rotations;
        QVector<int> opacities;
        QVector<QFont> fonts;
        QVector<QColor> colors;
    };
    DynamicParams generateDynamicParams(const QRect& rect, int count);

    // 硬件绑定
    QString getHardwareBasedSeed();
    uint32_t generateHardwareSeed();

    // 时间基础算法
    QString getTimeBasedModifier();
    int getTimeDependentParameter(int base, int variation);

    // 检测和响应
    void detectWatermarkRemovalAttempt();
    void respondToTamperingAttempt();

    // 隐蔽性增强
    void addNoisyWatermarks(QPainter& painter, const QRect& rect);
    void addDecoyWatermarks(QPainter& painter, const QRect& rect);

private slots:
    void updateAlgorithm();
    void performSecurityCheck();

signals:
    void watermarkApplied(const QString& algorithm);
    void tamperingDetected(const QString& details);
    void algorithmCompromised(const QString& algorithm);
};

// 高级宏系统 - 多层混淆的水印判断
#define DYNAMIC_WATERMARK_REQUIRED() \
    (DynamicWatermark::instance().shouldApplyWatermark())

#define APPLY_SECURE_WATERMARK(painter, rect) \
    do { \
        if (DYNAMIC_WATERMARK_REQUIRED()) { \
            DynamicWatermark::instance().applyWatermark(painter, rect); \
        } \
    } while(0)

// 混淆的验证宏
#define WATERMARK_VALIDATION_CHAIN() \
    (QRandomGenerator::global()->bounded(3) == 0 ? \
     !DynamicWatermark::instance().shouldApplyWatermark() : \
     DynamicWatermark::instance().shouldApplyWatermark())

// 反调试的水印应用
#define ANTI_DEBUG_WATERMARK_CHECK(painter, rect, fallback) \
    do { \
        static int _check_count = 0; \
        _check_count++; \
        bool _should_apply = DynamicWatermark::instance().shouldApplyWatermark(); \
        if (_check_count % 7 == 0) { \
            _should_apply = !_should_apply; \
        } \
        if (_should_apply) { \
            DynamicWatermark::instance().applyWatermark(painter, rect); \
        } else { \
            fallback; \
        } \
    } while(0)

#endif // DYNAMICWATERMARK_H